Case class is also a class, which is defined with “case” modifier. Scala
case classes are just regular classes which are immutable by default
and decomposable through pattern matching. It uses equal method to
compare instance structurally. It does not use new keyword to
instantiate object. All the parameters listed in the case class are public and immutable by default.
Syntax
Case class className(parameters)
case class Person(name: String,age:Int)
case object
A Case Object is also like an object, which has more
attributes than a regular Object. A case object has some more features than a regular object.Case object is also an object which is defined with “case” modifier.
Below two are important features of case object:
- It is serializable.
- It has a by default hashCode implementation.
Benefit of case class
Defining a class as a case class results in a lot of boilerplate
code being generated, with the following benefits:
- An
keyword to create a new instance of the class.
- Accessor methods are generated for the constructor parameters
because case class constructor parameters are val
by default. Mutator methods are also generated for parameters declared as
var
- A good, default
toString
method is generated. - An unapply
method is generated, making it easy to use case classes in match expression
s.
- equals and hashCode
methods are generated.
- A
copy method is generated.
Scala Compiler adds a
factory method with the name of the class with same number of parameters
defined in the class definition.
case class Person(name:String, age:Int)
object test {
case class Person(name: String,age:Int)
def main(args: Array[String]): Unit ={
val person1 = Person("Jhon",30)
val person2 = new Person("Tom",30)
println(person1)
println(person2)
}
}
Person(Jhon,30)
Person(Tom,30)
Person(Tom,30)
If we observe above code snippet, we can create Case class objects
without using “new” keyword. It is also allowed “new” keyword, but not
recommended to use it here.
Accessing the case class members
object test {
case class Person(name: String,age:Int)
def main(args: Array[String]): Unit ={
val person1 = Person("Jhon",30)
val person2 = new Person("Tom",30)
println(person1.name)
println(person2.age)
}
}
Case classes support pattern matching. So, you can use that in patterns. Following is the example of case classes and pattern.
Copying
You can create a (shallow) copy of an instance of a case class simply by using the copy method. You can optionally change the constructor arguments.
case class Student(name:String, marks:Int)
object Demo{
def main(args:Array[String]){
val s1 = Student("Rams",550)
val s2 = s1.copy()
println(s2)
println(s1==s2)
}
}
The recipient of message4 is used as the sender of message5 but the body of message4 was copied directly.
Scala Case Class and Pattern Matching Example
A case class which has no arguments is declared as case object instead of case class. case object is serializeable by default.
trait SuperTrait
case class CaseClass1(a:Int,b:Int) extends SuperTrait
case class CaseClass2(a:Int) extends SuperTrait // Case class
case object CaseObject extends SuperTrait // Case object
object MainObject{
def main(args:Array[String]){
callCase(CaseClass1(10,10))
callCase(CaseClass2(10))
callCase(CaseObject)
}
def callCase(f:SuperTrait) = f match{
case CaseClass1(f,g)=>println("a = "+f+" b ="+g)
case CaseClass2(f)=>println("a = "+f)
case CaseObject=>println("No Argument")
}
}
Output:
a = 10 b =10
a = 10
No Argument
Comparison
Case classes are compared by structure and not by reference:
object test {
case class Message(sender: String, recipient: String, body: String)
def main(args: Array[String]) {
val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val messagesAreTheSame = message2 == message3 // true
println(messagesAreTheSame)
}
}
Even though message2 and message3 refer to different objects, the value of each object is equal.
How To create Jar File form command prompt
scalac hello.scala -d hello.jar
To run Spark Job
spark-submit --master yarn-client --class "Hello" /home/hduser/Desktop/scala/hello.jar
No comments:
Post a Comment